package com.supergzt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.supergzt.entity.dos.Bill;
import com.supergzt.entity.dos.Budget;
import com.supergzt.entity.vos.BudgetQueryVo;
import com.supergzt.entity.vos.NoticeVo;
import com.supergzt.mapper.BillMapper;
import com.supergzt.service.BudgetService;
import com.supergzt.mapper.BudgetMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

@Service
public class BudgetServiceImpl extends ServiceImpl<BudgetMapper, Budget>
		implements BudgetService {

	@Resource
	private BudgetMapper budgetMapper;

	@Resource
	private BillMapper billMapper;

	@Override
	public Page<Budget> queryBudget(BudgetQueryVo budgetQueryVo, Page<Budget> page) {
		LocalDate budgetDate = budgetQueryVo.getBudgetDate();
		if(budgetDate != null) {
			return page(page,new QueryWrapper<Budget>().eq("budget_date",budgetDate));
		}
		return null;
	}

	@Override
	public NoticeVo getBudgetStatus() {
		LocalDate now = LocalDate.now();
		LocalDate start = now.withDayOfMonth(1);
		List<Budget> budgets = budgetMapper.selectList(new QueryWrapper<Budget>().ge("budget_date", start).le("budget_date", now));
		BigDecimal totalBudgetAmount = new BigDecimal("0");
		for (Budget budget : budgets) {
			totalBudgetAmount = totalBudgetAmount.add(budget.getAmount());
		}
		List<Bill> bills = billMapper.selectList(new QueryWrapper<Bill>().ge("bill_time", start).le("bill_time", now));
		BigDecimal totalCost = new BigDecimal("0");
		for (Bill bill : bills) {
			totalCost = totalCost.add(bill.getAmount());
		}
		BigDecimal subtract = totalBudgetAmount.subtract(totalCost);
		return new NoticeVo().setAmount(subtract.abs().intValue()).setFlag(totalBudgetAmount.compareTo(totalCost) + 1).setMonth(now.getMonthValue());
	}
}




